#!/bin/bash

set -e
readonly CURRENT_PATH=$(cd "$(dirname "$0")"; pwd)
INSTALL_PATH="${CURRENT_PATH}/../install"
OPENSOURCE_PATH="${CURRENT_PATH}/../3rdparty"

[ -d "${INSTALL_PATH}" ] && rm -rf "${INSTALL_PATH}"
mkdir -p "${INSTALL_PATH}"

SECUREC_SRC_PATH="${OPENSOURCE_PATH}/huawei_secure_c/src"
echo "Build ${SECUREC_SRC_PATH}"
SECUREC_INSTALL_PATH="${INSTALL_PATH}/securec"

function compile_securec()
{
    [ -d "${SECUREC_INSTALL_PATH}" ] && rm -rf "${SECUREC_INSTALL_PATH}"
    echo "${SECUREC_INSTALL_PATH}"
    if [[ !-d "${SECUREC_INSTALL_PATH}" ]]; then
        mkdir -p "${SECUREC_INSTALL_PATH}"
    fi

    cd "${SECUREC_SRC_PATH}"
    make -j 10
    make lib -j 10
    cp -rf "${OPENSOURCE_PATH}/huawei_secure_c/include" "${SECUREC_INSTALL_PATH}/"
    cp -rf "${OPENSOURCE_PATH}/huawei_secure_c/lib" "${SECUREC_INSTALL_PATH}/"

}

function compile_openssl(){
    echo "making openssl patch"
    cmake ${OPENSOURCE_PATH}
}

compile_securec
echo "compile huawei_secure_c"
compile_openssl
echo "compile huawei_openssl"